<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  News_category_model extends Model 
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ;  
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */
	function News_category_model()
	{
		parent::Model();
	}
	
	function insertGroupUser($user_id, $group_id, $permission_id, $color_id, $active){
		$sql = "INSERT IGNORE INTO user_group (user_id, group_id, permission_id, color_id, active) VALUES ($user_id, $group_id, $permission_id, $color_id, $active)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function create($category){
		$sql = "INSERT INTO news_category (category_parent_id, name_vi, name_en, description_vi, description_en, picture, position, active)
				VALUE
				($category->category_parent_id, '$category->name_vi', '$category->name_en', '$category->description_vi', '$category->description_en', '$category->picture', $category->position, $category->active)";
		return $this->db->query($sql);
	}
	
	function update($category){
		echo $sql = "UPDATE news_category 
				SET 
				category_parent_id = $category->category_parent_id, 
				name_vi = '$category->name_vi', 
				name_en = '$category->name_en', 
				description_vi = '$category->description_vi', 
				description_en = '$category->description_en',
				picture = '$category->picture', 
				position = $category->position, 
				active = $category->active
				WHERE category_id = $category->category_id";
		return $this->db->query($sql);
	}
	
	function getAll(){
		$result = null;
		$sql = "SELECT *
				FROM news_category";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM news_category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($category_id){
		
		$sql = "SELECT *
				FROM news_category 
				WHERE category_id = $category_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function isInGroup($user_id, $group_id){
		$sql = "SELECT 1 as is_member FROM user_group ug
				INNER JOIN users u ON u.id = ug.user_id
				WHERE group_id = $group_id AND ug.user_id = $user_id";
		$query = $this->db->query($sql);
		if($query->result())
			return 1;
		
		return 0;
		
	}
	
	function changeColor($group_id, $user_id, $color_id){
		$sql = "Update user_group SET color_id = $color_id WHERE group_id = $group_id AND user_id = $user_id";
		$this->db->query($sql);
	}
	
	function changeStatus($user_id, $group_id, $status){
		$sql = "UPDATE FROM user_group SET active = $status WHERE user_id = $user_id AND group_id = $group_id";
		$this->db->query($sql);
	} 
	
	function changePermission($member_id, $group_id, $permission_id){
		$sql = "UPDATE user_group SET permission_id = $permission_id WHERE user_id = $member_id AND group_id = $group_id";
		$this->db->query($sql);
	}
	
	function reject($group_id, $user_id){
		$sql = "DELETE FROM user_group WHERE user_id = $user_id AND group_id = $group_id";
		$this->db->query($sql);
	}
	
	//function getUserBy
	
//	
}

// EOF
